package com.julian.delegatePattern.virtualProxy;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Hashtable;

public class ImageProxyStateTestDrive {
    ImageComponent imageComponent;
    JFrame jFrame = new JFrame("CD Cover Viewer");//set the title for the frame
    JMenuBar jMenuBar;
    JMenu jMenu;
    Hashtable cds = new Hashtable();

    public static void main(String[] args) throws Exception {
        ImageProxyStateTestDrive imageProxyTestDrive = new ImageProxyStateTestDrive();
    }

    public ImageProxyStateTestDrive() throws Exception {
        cds.put("Ballet Pose","https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1736727202,1355489933&fm=26&gp=0.jpg");
        cds.put("Naruto","https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1062924988,1223376387&fm=26&gp=0.jpg");
        cds.put("Selected Ambient Works, Vol. 2","https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1265870196,2883097596&fm=26&gp=0.jpg");
        cds.put("kimonoBaiDu","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598667455000&di=c90ebc533f1a4e1109a18d52ee353ee7&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F454d60b63dbc3858331bbc4bc8757aef329e5b59231b6-bcV3m4_fw658");
        //重复八次

        URL initialURL = new URL((String)cds.get("Selected Ambient Works, Vol. 2"));
        jMenuBar = new JMenuBar();
        jMenu = new JMenu("fAVORITE cds");//set the title of the menu
        jMenuBar.add(jMenu);
        jFrame.setJMenuBar(jMenuBar);
        for (Enumeration e = cds.keys();e.hasMoreElements();){
            String name = (String)e.nextElement();
            JMenuItem jMenuItem = new JMenuItem(name);
            jMenu.add(jMenuItem);
            jMenuItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    imageComponent.setIcon(new ImageProxyState(getCDUrl(e.getActionCommand())));
                    jFrame.repaint();
                }
            });
        }

        //建立框架和菜单
        Icon icon = new ImageProxyState(initialURL);
        imageComponent = new ImageComponent(icon);
        jFrame.getContentPane().add(imageComponent);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setSize(1000,800);
        jFrame.setVisible(true);
        jFrame.setLocation(450,100);
    }

    URL getCDUrl(String name){
        try {
            return new URL((String)cds.get(name));
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        }
    }
}
